﻿using System.Collections.Generic;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using Clandestine.Entities;

namespace Slick.MapEditor.Tools
{
    public class EntitySelecter : ITool
    {
        public Map Map;
        public string Name { get { return "Entity Selecter"; } }

        public EntitySelecter(Map map)
        {
            this.Map = map;
        }

        public void Focus()
        {
        }

        public void Unfocus()
        {
        }

        public void MouseUpLeft(int mouseX, int mouseY, int tileX, int tileY)
        {

        }

        public void MouseDownLeft(int mouseX, int mouseY, int tileX, int tileY)
        {
            // Left-click.
            // We'll set the current entity as the first one we find
            // occupying the given coordinates.
            // Search backwards, so newer (and probably higher)
            // things have precedence.
            lock (Map.Entities)
                for (int i = Map.Entities.Count - 1; i >= 0; i--)
                    if (Intersects(new Rectangle(Map.Entities[i].Position, Map.Entities[i].Size),
                        new Rectangle(mouseX, mouseY, 1, 1)))
                    {
                        Map.Entities.SelectEntity(Map.Entities[i]);
                        return;
                    }
        }

        public void MouseUpRight(int mouseX, int mouseY, int tileX, int tileY)
        {

        }


        public void MouseDownRight(int mouseX, int mouseY, int tileX, int tileY)
        {
            // Provide a list of entities
            lock (Map.Entities)
            {
                // get list of potential entities (i.e. the one the mouse is over)
                List<Entity> potentialEntities = new List<Entity>();
                foreach (Entity e in Map.Entities)
                {
                    if (Intersects(new Rectangle(e.Position, e.Size),
                        new Rectangle(mouseX, mouseY, 1, 1)))
                    {
                        potentialEntities.Add(e);
                    }
                }

                // check we have some potential entities
                if (potentialEntities.Count == 0)
                    MessageBox.Show("No entities exist in that spot.", "No entities found",
                        MessageBoxButtons.OK, MessageBoxIcon.Warning);
                else
                {
                    throw new System.NotImplementedException();
                    /*// Disable the main form so we can do some work!
                    Program.fMain.Invoke(new ThreadStart(delegate()
                    {
                        Program.fMain.Enabled = false;
                    }));

                    // New search results window and populate it
                    frmEntitySearchResults fEsr = new frmEntitySearchResults();
                    foreach (Entity e in potentialEntities)
                        fEsr.lstResults.Items.Add(e.ToString());

                    // Show the dialog and check it all went okay.
                    // We'll be given an entity to select :)
                    if (fEsr.ShowDialog() == DialogResult.OK)
                        if (fEsr.lstResults.SelectedIndex > -1)
                            Entities.SelectEntity(potentialEntities[fEsr.lstResults.SelectedIndex]);

                    // Re-enable the main form
                    Program.fMain.BeginInvoke(new ThreadStart(delegate()
                    {
                        Program.fMain.Enabled = true;
                    }));*/
                }
            }
        }

        public void MouseMove(int mouseX, int mouseY, int tileX, int tileY)
        {
        }

        private bool Intersects(Rectangle tthis, Rectangle value)
        {
            return (tthis.Bottom > value.Top && tthis.Top < value.Bottom && tthis.Left < value.Right
                && tthis.Right > value.Left);
        }

    }
}
